<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "prf_score_exchange".
 *
 * @property integer $id
 * @property integer $item_id
 * @property integer $integral
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $user_max_number
 * @property integer $total
 * @property integer $last_total
 * @property string $title
 * @property integer $freeShipping
 */
class PrfScoreExchange extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'prf_score_exchange';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        		[['item_id', 'integral',  'user_max_number', 'total', 'title'], 'required'],
            [['item_id', 'integral', 'user_max_number', 'total', 'last_total', 'freeShipping'], 'integer'],
        		[['item_id'], 'exist','targetClass' => '\backend\models\SysItem', 'targetAttribute'=>'id','message' => '单品不存在!'],
            [['title'], 'string', 'max' => 255],
        		['last_total', 'validataLasttotal'],
        ];
    }
    /**
     * 剩余总数不可大于总数
     * @param unknown $attribute
     */
    public function validataLasttotal($attribute) {
    	if ($this->last_total >$this->total) {
    		$this->addError($attribute, '剩余总数不可大于总件数！');
    	}
    }
    
    

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'item_id' => '单品ID',
            'integral' => '所需积分',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'user_max_number' => '每用户最大兑换数',
            'total' => '总件数',
            'last_total' => '剩余总数',
            'title' => '活动名称',
            'freeShipping' => '是否包邮',
        ];
    }

    public function getImg0()
    {
        return $this->hasOne(Image::className(), ['id' => 'img_small_id'])->viaTable(SysItem::tableName(),['id' => 'item_id']);
    }

    public function getItem0()
    {
        return $this->hasOne(SysItem::className(), ['id' => 'item_id']);
    }
}
